package com.fingard.dsp.bank.directbank.nnbill01;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.ReqOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.RetAtsOtherElse;
import com.fingard.text.MD5SaltHelper;
import com.fingard.text.StringHelper;

import java.util.HashMap;
import java.util.Map;

public class NNBill01OtherElse extends NNBill01Base implements IOtherElse {
	@Override
	public RetAtsOtherElse d_else(ReqOtherElse p_reqElse) throws Exception {
		String transType = p_reqElse.reqHeader.transType;
		
		if ("VerifySign".equals(transType)) {
			// 人民币网关支付验签
			return verifyRMBWG(p_reqElse);
		} else if ("UnifiedSign".equals(transType)) {
			if ("POS".equals(p_reqElse.reqHeader.commandCode)) {
				return unifiedSignForPos(p_reqElse);
			}
		} else {
			RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
			
			retAts.retHashMap = new HashMap<String, String>();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", "未知的TransType：" + transType);
			
			return retAts;
		}
		
		return null;
	}
	
	public RetAtsOtherElse verifyRMBWG(ReqOtherElse p_reqElse) {
		RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);

		retAts.retHashMap = new HashMap<String, String>();
		try {
			String ownAct = "";
			if (p_reqElse.reqHashMap.containsKey("OurBankAccount")) {
				ownAct = p_reqElse.reqHashMap.get("OurBankAccount");
			}
			
			String merchantId = "";
			if (p_reqElse.reqHashMap.containsKey("MchId")) {
				merchantId = p_reqElse.reqHashMap.get("MchId");
			}
			
			String appId = "";
			if (p_reqElse.reqHashMap.containsKey("AppId")) {
				appId = p_reqElse.reqHashMap.get("AppId");
			}
			
			String plain = "";
			if (p_reqElse.reqHashMap.containsKey("Plain")) {
				plain = p_reqElse.reqHashMap.get("Plain");
			} else {
				retAts.retHashMap.put("RespCode", "-1");
				retAts.retHashMap.put("RespInfo", "缺失验签原始串");

				return retAts;
			}

			ActSetItem actItem = this.getBankConfig().getActSetItemByAny(ownAct, merchantId, appId, "*");
			if (actItem != null) {
				
				String[] retStr = getServerSign(plain);
				WriteBankLogLn2("快钱返回待验签串为:\r\n" + retStr[0]);
				WriteBankLogLn2("快钱返回签名串为\r\n:" + retStr[1]);
				
				// 执行验签
				boolean useNewSign = getBankConfig().getValueAsBool("useNewSign");
				boolean verifySignResult = false;
				if (useNewSign) {
					verifySignResult = enCodeByCer(retStr[0], retStr[1], actItem);
				} else {
					verifySignResult = verifyRMBGW(retStr[0], retStr[1], actItem);
				}
				if (verifySignResult) {
					retAts.retHashMap.put("RespCode", "0");
					retAts.retHashMap.put("RespInfo", "验签通过");
				} else {
					retAts.retHashMap.put("RespCode", "-1");
					retAts.retHashMap.put("RespInfo", "验签失败");
				}
			} else {
				retAts.retHashMap.put("RespCode", "-1");
				retAts.retHashMap.put("RespInfo", "未找到账号[" + ownAct + "]商户配置信息");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", ex.getMessage());
			WriteBankLogLn(ex);
		}

		return retAts;
	}
	
	/**
	 * POS保单信息查询签名
	 * 
	 * @param p_reqElse
	 * @return
	 */
	public RetAtsOtherElse unifiedSignForPos(ReqOtherElse p_reqElse) {
		RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
		retAts.retHashMap = new HashMap<String, String>();

		try {
			Map<String, String> mapParams = p_reqElse.reqHashMap;
			mapParams = removeHead(mapParams);

			String tmpSignData = getSignStr(mapParams);
			tmpSignData += "&key=" + getBankConfig().getValueAsString("saltKey");
			WriteBankLogLn2("待签名数据:\r\n" + tmpSignData);
			String sign = MD5SaltHelper.md5(tmpSignData, getCharset("UTF-8", "pos"));
			WriteBankLogLn2("签名结果:"+sign);
			if (StringHelper.hasAnyChar(sign)) {
				retAts.retHashMap.put("RespCode", "0");
				retAts.retHashMap.put("RespInfo", "");
				retAts.retHashMap.put("Result", sign);
			} else {
				retAts.retHashMap.put("RespCode", "-1");
				retAts.retHashMap.put("RespInfo", "签名失败!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", ex.getMessage());
			WriteBankLogLn(ex);
		}

		return retAts;
	}
}
